home *** CD-ROM | disk | FTP | other *** search
- // Gmail Manager
- // By Todd Long <longfocus@gmail.com>
- // http://www.longfocus.com/firefox/gmanager/
-
- const GM_CC = Components.classes;
- const GM_CI = Components.interfaces;
-
- const GM_BRANCH = "longfocus.gmanager.";
- const GM_PASSWORD_SITE = "longfocus.gmanager.account";
- const GM_PROPERTIES = "chrome://gmanager/locale/gmanager.properties";
- const GM_VERSION = "0.5.1";
-
- function gmManager() {}
- gmManager.prototype = {
- _firstTime: true,
- _parser: null,
- _global: null,
- _accounts: null,
-
- get branch() {
- return GM_CC["@mozilla.org/preferences-service;1"]
- .getService(GM_CI.nsIPrefService)
- .getBranch(GM_BRANCH);
- },
-
- get parser() {
- if (!this._parser) {
- try {
- this._parser = GM_CC["@longfocus.com/gmanager/parser;1"].getService(GM_CI.gmIParser);
- this._parser.load();
- } catch(e) {
- this._parser = null;
- }
- }
- return this._parser;
- },
-
- get version() {
- return GM_VERSION;
- },
-
- load: function()
- {
- if (this._firstTime)
- {
- // Load accounts
- this._loadAccounts();
-
- // Set first time
- this._firstTime = false;
- }
- },
-
- save: function()
- {
- for (var email in this._accounts)
- {
- var account = this._accounts[email];
- var oldNode = account.prefs.node;
- var newNode = null;
-
- // Save preferences
- account.prefs.save();
-
- // Get new node
- newNode = account.prefs.node;
-
- if (newNode.getAttribute("type") != "global")
- {
- newNode.setAttribute("type", account.type);
- newNode.setAttribute("email", account.email);
- newNode.setAttribute("alias", account.alias);
-
- if (account.remember)
- this._storePassword(account.email, account.password);
- else
- this._removePassword(account.email);
- }
-
- // Replaces old node
- this.parser.doc.replaceChild(newNode, oldNode);
- }
-
- // Save file
- this.parser.save(null);
- },
-
- importFile: function(aWindow, aFile)
- {
- var file = this._filePicker(aWindow, "import");
-
- if (file != null)
- {
- if (this.parser.open(file))
- {
- // Load accounts
- this._loadAccounts();
-
- // Save accounts
- this.save();
- }
- else
- file = "";
- }
-
- return file;
- },
-
- exportFile: function(aWindow)
- {
- var file = this._filePicker(aWindow, "export");
-
- if (file != null)
- this.parser.save(file);
-
- return file;
- },
-
- getAccounts: function(aCount)
- {
- var accounts = new Array();
-
- for (var email in this._accounts)
- accounts.push(this._accounts[email]);
-
- aCount.value = accounts.length;
-
- return accounts;
- },
-
- getAccount: function(aEmail)
- {
- return this._accounts[aEmail];
- },
-
- addAccount: function(aType, aEmail, aPassword, aRemember, aAlias)
- {
- // Get account node
- var node = this.parser.account;
-
- // Add node
- this.parser.doc.appendChild(node);
-
- // Add account
- this._accounts[aEmail] = this._createService(aType, aEmail, aPassword, aRemember, aAlias, node);
-
- // Save
- this.save();
-
- return true;
- },
-
- removeAccount: function(aEmail)
- {
- // Removes the account from document
- this.parser.doc.removeChild(this._accounts[aEmail].prefs.node);
-
- // Removes the account from list
- delete this._accounts[aEmail];
-
- // Save accounts
- this.save();
- },
-
- updateAccount: function(aEmail, aPassword, aRemember, aAlias)
- {
- // Update account
- this._accounts[aEmail].password = aPassword;
- this._accounts[aEmail].remember = aRemember;
- this._accounts[aEmail].alias = aAlias;
-
- // Save accounts
- this.save();
- },
-
- _loadAccounts: function()
- {
- var accounts = this.parser.doc.getElementsByTagName("account");
-
- // Create our list of accounts
- this._accounts = new Array();
-
- for (var i = 0; i < accounts.length; i++)
- {
- // Account details
- var node = accounts.item(i);
- var type = node.getAttribute("type");
- var email = "global";
- var password = null;
- var remember = null;
- var alias = null;
-
- if (type != "global")
- {
- email = node.getAttribute("email");
- password = node.getAttribute("password");
- alias = node.getAttribute("alias");
-
- if (password == null)
- password = this._findPassword(email);
- else
- {
- // Remove password attribute
- node.removeAttribute("password");
-
- if (password != "")
- this._storePassword(email, password)
- else
- this._removePassword(email);
- }
-
- remember = (password != "" ? true : false);
- }
-
- this._accounts[email] = this._createService(type, email, password, remember, alias, node);
- }
- },
-
- _createService: function(aType, aEmail, aPassword, aRemember, aAlias, aNode)
- {
- var service = null;
- var prefs = GM_CC["@longfocus.com/gmanager/prefs;1"].createInstance(GM_CI.gmIPrefs);
-
- try {
- // Check service type
- switch (aType)
- {
- case "global":
- // Create service
- service = GM_CC["@longfocus.com/gmanager/service;1"].createInstance(GM_CI.gmIService);
- break;
- case "gmail":
- // Create Gmail service
- service = GM_CC["@longfocus.com/gmanager/service/gmail;1"].createInstance(GM_CI.gmIServiceGmail);
- break;
- default:
- break;
- }
-
- // Prefs node
- prefs.node = aNode;
-
- // Service details
- service.type = aType;
- service.email = aEmail;
- service.password = aPassword;
- service.remember = aRemember;
- service.alias = aAlias;
- service.prefs = prefs;
- } catch(e) {
- service = null;
- }
-
- return service;
- },
-
- _findPassword: function(aEmail)
- {
- try {
- var host = {value:""};
- var user = {value:""};
- var password = {value:""};
- var pmi = GM_CC["@mozilla.org/passwordmanager;1"].getService(GM_CI.nsIPasswordManagerInternal);
- pmi.findPasswordEntry(GM_PASSWORD_SITE, aEmail, "", host, user, password);
- } catch(e) {}
-
- return password.value;
- },
-
- _storePassword: function(aEmail, aPassword)
- {
- try {
- var pm = GM_CC["@mozilla.org/passwordmanager;1"].getService(GM_CI.nsIPasswordManager);
-
- if (aPassword != null && aPassword != "")
- pm.addUser(GM_PASSWORD_SITE, aEmail, aPassword);
- } catch (e) {}
- },
-
- _removePassword: function(aEmail)
- {
- try {
- var pm = GM_CC["@mozilla.org/passwordmanager;1"].getService(GM_CI.nsIPasswordManager);
- pm.removeUser(GM_PASSWORD_SITE, aEmail);
- } catch (e) {}
- },
-
- _filePicker: function(aWindow, aMode)
- {
- var nsIFilePicker = GM_CI.nsIFilePicker;
- var picker = GM_CC["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
- var bundle = GM_CC["@mozilla.org/intl/stringbundle;1"].getService(GM_CI.nsIStringBundleService).createBundle(GM_PROPERTIES);
- var file = null;
-
- picker.appendFilters(nsIFilePicker.filterXML);
- picker.defaultExtension = ".xml";
-
- switch (aMode)
- {
- case "export":
- picker.init(aWindow, bundle.GetStringFromName("imports-prefs-export"), nsIFilePicker.modeSave);
- break;
- case "import":
- picker.init(aWindow, bundle.GetStringFromName("imports-prefs-import"), nsIFilePicker.modeOpen);
- break;
- };
-
- if (picker.show() == nsIFilePicker.returnOK)
- file = picker.file;
-
- return file;
- },
-
- QueryInterface: function(iid)
- {
- if (!iid.equals(GM_CI.gmIManager) &&
- !iid.equals(GM_CI.nsISupports))
- throw Components.results.NS_ERROR_NO_INTERFACE;
- return this;
- }
- }
-
- var myModule = {
- firstTime: true,
-
- myCID: Components.ID("{bf43b6d0-f7dd-11da-974d-0800200c9a66}"),
- myDesc: "Mail accounts manager",
- myProgID: "@longfocus.com/gmanager/manager;1",
- myFactory: {
- createInstance: function (outer, iid) {
- if (outer != null)
- throw Components.results.NS_ERROR_NO_AGGREGATION;
-
- return (new gmManager()).QueryInterface(iid);
- }
- },
-
- registerSelf: function (compMgr, fileSpec, location, type)
- {
- if (this.firstTime) {
- this.firstTime = false;
- throw Components.results.NS_ERROR_FACTORY_REGISTER_AGAIN;
- }
-
- compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
- compMgr.registerFactoryLocation(this.myCID, this.myDesc, this.myProgID, fileSpec, location, type);
- },
-
- getClassObject: function (compMgr, cid, iid)
- {
- if (!cid.equals(this.myCID))
- throw Components.results.NS_ERROR_NO_INTERFACE;
-
- if (!iid.equals(Components.interfaces.nsIFactory))
- throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
-
- return this.myFactory;
- },
-
- canUnload: function(compMgr) { return true; }
- };
-
- function NSGetModule(compMgr, fileSpec) { return myModule; }